<template>
  <div class="app-container">
    <!-- 数据表格 -->
    <el-table :data="data" style="width: 100%" border>
      <el-table-column prop="index" label="序号" width="60" align="center">
        <template slot-scope="scope">{{ scope.$index + 1 }}</template>
      </el-table-column>

      <el-table-column prop="title" label="标题" width="150" align="center">
        <template slot-scope="scope">{{ scope.row.title }}</template>
      </el-table-column>

      <el-table-column prop="description" label="描述" align="center">
        <template slot-scope="scope">{{ scope.row.description }}</template>
      </el-table-column>

      <el-table-column label="中图预览" width="150" align="center">
        <template slot-scope="scope">
          <el-image
            style="width: 100px"
            :src="scope.row.midImg2"
            fit="fill"
            :preview-src-list="midsrcList"
          ></el-image>
        </template>
      </el-table-column>

      <el-table-column label="大图预览" width="150" align="center">
        <template slot-scope="scope">
          <el-image
            style="width: 100px"
            :src="scope.row.bigImg2"
            fit="fill"
            :preview-src-list="bigsrcList"
          ></el-image>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" width="150">
        <template slot-scope="scope">
          <el-tooltip
            class="item"
            effect="dark"
            content="编辑"
            placement="top"
            :hide-after="2000"
          >
            <el-button
              type="primary"
              icon="el-icon-edit"
              circle
              size="mini"
              @click="editBannerHandle(scope.row)"
            ></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <!-- 编辑框 -->
    <el-dialog title="请编辑信息" :visible.sync="dialogFormVisible" top="5px">
      <el-form :model="form">
        <!-- 标题 -->
        <el-form-item label="标题">
          <el-input v-model="form.title"></el-input>
        </el-form-item>
        <!-- 描述 -->
        <el-form-item label="描述">
          <el-input v-model="form.description"></el-input>
        </el-form-item>

        <el-row>
          <el-col :span="12">
            <el-form-item label="中图预览">
              <!-- 中图 -->
              <Upload v-model="form.midImg" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="大图预览">
              <!-- 大图 -->
              <Upload v-model="form.bigImg" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="editBannerConfirm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { setBanner, getBanner } from "@/api/banner";
import { server_url } from "@/urlConfig";
import Upload from "@/components/Upload";
export default {
  data() {
    return {
      data: [],
      dialogFormVisible: false, //编辑对话框是否显示
      form: {
        id: "",
        title: "",
        midImg: "",
        bigImg: "",
        description: "",
      },
      midsrcList: [], //存放所有中图
      bigsrcList: [],
    };
  },
  components: {
    Upload,
  },
  created() {
    //创建时先获取一次数据
    this.fetchData();
  },
  methods: {
    //获取首页标语
    fetchData() {
      getBanner().then((res) => {
        for (var item of res.data) {
          //给图片路径前面加上服务器地址
          item.midImg2 = server_url + item.midImg;
          item.bigImg2 = server_url + item.bigImg;
          this.midsrcList.push(item.midImg2);
          this.bigsrcList.push(item.bigImg2);
        }
        this.data = res.data;
      });
    },

    // 编辑按钮打开事件
    editBannerHandle(bannerInfo) {
      //拿到该条数据并赋值给form,采用结构可以使两个变量分别指向两块不同位置
      this.form = { ...bannerInfo };
      //显示编辑对话框
      this.dialogFormVisible = true;
    },

    //编辑对话框确定事件
    editBannerConfirm() {
      // 从表单里面拿到用户修改的数据，发送给服务器
      // 因为 api 文档要求三个首页标语都要发送过去，哪怕只改了其中一个
      let arr = [...this.data];
      for (var i = 0; i < arr.length; i++) {
        if (arr[i].id === this.form.id) {
          arr[i] = this.form;
        }
      }
      //设置首页标语
      setBanner(arr).then((res) => {
        //关闭编辑对话框
        this.dialogFormVisible = false;
        this.$message({ message: "修改成功", type: "success" });
        //重新获取首页标语
        this.fetchData();
      });
    },
  },
  //上传首页标语（需要上传一整个数组）
};
</script>

<style></style>
